package com.ask.socket;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class listenSocket implements Runnable {
	private Socket socket = null;
	private Thread thread = null;

	public listenSocket() {
		thread = new Thread(this);
		thread.start();
	}

	public listenSocket(Socket serverSocket) throws IOException {
		socket = serverSocket;
		thread = new Thread(this);
		thread.start();
	}

	@Override
	public synchronized void run() {
		try {
			// KnockKnockProtocol kkp = new KnockKnockProtocol();
			// outputLine = //kkp.processInput(null);
			DataOutputStream out = new DataOutputStream(
					socket.getOutputStream());
			BufferedReader in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));

			String Inline = "";
			out.writeBytes("keluar while dari server\n");
			while (Inline != null) {
				Inline = in.readLine();
				System.out.println(Inline + " :: From Client");
				// KnockKnockProtocol kkp = new KnockKnockProtocol();
				// outputLine = //kkp.processInput(null);
				if (Inline.equals("Bye")) {
					break;
				}
				if (Inline.equals("")) {
					System.out.println("null Nothing Empty!!!");
					Inline = null;
				}

			}
			System.out.println("before socket close");
			socket.close();

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public String strOut() {
		return "oke";
	}
	
	public void setSocket(Socket sckt){
		socket = sckt;
	}
}
